//
//  VC_Signup.m
//  yjw-winner
//
//  Created by 王春辉 on 2018/9/13.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_Signup.h"
#import "Cell_Signup.h"
#import "Cell_Signuped.h"

static NSString *signupIdentifier = @"signup_identifier";
static NSString *signupedIdentifier = @"siguped_identifier";
@interface VC_Signup ()<UITableViewDelegate, UITableViewDataSource> {
    UITableView *_tableView;
}
@property (nonatomic, strong) UIView *siginView;
@property (nonatomic, strong) NSArray *btns;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation VC_Signup

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@[@"",
                         @"",
                         @""],
                       @[@"",
                         @"",
                         @""]];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的报名";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    [self initButtons];
    [self initSiginView];
    [self initTableView];
}

- (void)initButtons {
    NSArray *titles = @[@"商学院"];
    NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:titles.count];
    for (int i = 0; i < titles.count; i++) {
        NSString *title = titles[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:MJRefreshColor(220, 188, 151) forState:UIControlStateSelected];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(segmentButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [mArr addObject:button];
        button.frame = CGRectMake(self.view.bounds.size.width / 3 * i, 0, self.view.bounds.size.width / 3, 50);
        button.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:button];
        if (i == 0) {
            button.selected = YES;
        }
    }
    self.btns = [NSArray arrayWithArray:mArr];
}
- (void)initSiginView {
    self.siginView = [[UIView alloc] init];
    self.siginView.backgroundColor = MJRefreshColor(220, 188, 151);
    UIButton *btn = self.btns.firstObject;
    CGFloat w = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}].width;
    self.siginView.bounds = CGRectMake(0, 0, w, 1);
    [self.view addSubview:self.siginView];
    self.siginView.center = CGPointMake(self.view.bounds.size.width / 6, 50);
}
/**
 初始化tableView
 */
- (void)initTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.mj_w, self.view.mj_h - 50 - 64) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = MJRefreshColor(242, 242, 242);
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([Cell_Signup class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:signupIdentifier];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([Cell_Signuped class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:signupedIdentifier];
}

#pragma mark --mark UITableViewDelegate UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        Cell_Signuped *cell = [tableView dequeueReusableCellWithIdentifier:signupedIdentifier forIndexPath:indexPath];
        if (!cell) {
            cell = [[Cell_Signuped alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:signupedIdentifier];
        }
        return cell;
    }else {
        Cell_Signup *cell = [tableView dequeueReusableCellWithIdentifier:signupIdentifier forIndexPath:indexPath];
        __weak typeof(self) weakself = self;
        [cell refreshWithModel:nil CallBackBlock:^(id model) {
            typeof(weakself) strongself = weakself;
            [strongself signupCellSignupButtonClickCallBackWith:model];
        }];
        if (!cell) {
            cell = [[Cell_Signup alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:signupIdentifier];
        }
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 120.0f;
    }else {
        return 105.0f;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arr = self.dataArray[section];
    return arr.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    @autoreleasepool {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.mj_w, 5.0f)];
        return view;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    @autoreleasepool {
        if (section == 1) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.mj_w, 40)];
            label.backgroundColor = [UIColor whiteColor];
            NSString *text = @"预约现场报名";
            label.font = [UIFont boldSystemFontOfSize:15.0f];
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.alignment = NSTextAlignmentLeft;
            style.headIndent = 0.0f;
            CGFloat emptylen = 10.0f;
            style.firstLineHeadIndent = emptylen;
            NSAttributedString *attriText = [[NSAttributedString alloc] initWithString:text attributes:@{NSParagraphStyleAttributeName:style}];
            label.attributedText = attriText;
            return label;
        }else {
            return [UIView new];
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 40.0f;
    }else {
        return CGFLOAT_MIN;
    }
}
#pragma mark --mark EVENT

- (void)segmentButtonClick:(UIButton *)sender {
    
}



#pragma mark --mark 业务逻辑

- (void)signupCellSignupButtonClickCallBackWith:(id)model {
    [MBProgressHUD showSuccess:@"我要报名"];
}
@end
